using OrderSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class HotelView : MonoBehaviour
{
    //public UnityAction CallWaiter = null;
    //public UnityAction<Order> Order = null;
    //public UnityAction Pay = null;
    //public UnityAction CallCook = null;
    public UnityAction<HotelItem> ServerFood = null;

    private ObjectPool<HotelItemView> objectPool = null;
    private List<HotelItemView> hotels = new List<HotelItemView>();
    private Transform parent = null;

    private void Awake()
    {
        parent = this.transform.Find("Content");
        var prefab = Resources.Load<GameObject>("Prefabs/UI/Hoteltem");
        objectPool = new ObjectPool<HotelItemView>(prefab, "HotelPool");
    }
    public void UpdateWaiter(IList<HotelItem> hotels)
    {
        for (int i = 0; i < this.hotels.Count; i++)
            objectPool.Push(this.hotels[i]);

        this.hotels.AddRange(objectPool.Pop(hotels.Count));
        Move(hotels);
    }

    public void Move(IList<HotelItem> waiters)
    {
        for (int i = 0; i < this.hotels.Count; i++)
        {
            this.hotels[i].transform.SetParent(parent);
            var item = waiters[i];
            this.hotels[i].transform.Find("Id").GetComponent<Text>().text = item.ToString();
            Color color = Color.white;
            if (item.state.Equals(0))
                color = Color.green;
            else if (item.state.Equals(1))
            {
                color = Color.yellow;
                StartCoroutine(WaiterServing(item));
            }
            else if (item.state.Equals(2))
                color = Color.red;
            this.hotels[i].GetComponent<Image>().color = color;
        }
    }

    IEnumerator WaiterServing(HotelItem item, float time = 4)
    {
        yield return new WaitForSeconds(time);
        ServerFood.Invoke(item);
    }
}
